﻿<?php

class PublicAction extends Action {
	// 检查用户是否登录
	protected function checkUser() {
		if(!isset($_SESSION[C('USER_AUTH_KEY')])) {
			$this->assign('jumpUrl','Public/login');
			$this->error('没有登录');
		}
	}


	// 项目管理菜单
    public function adminmenu() {
        if(isset($_SESSION[C('USER_AUTH_KEY')]) && 
            $_SESSION[C('USER_AUTH_VALUE')] == C('USER_AUTH_VALUE_ADMIN')) {
            $this->display();
        }else {
            //$this->redirect('Public/login');
            //$this->error('没有操作权限','Public/login');
            echo '<div class="accordion" fillSpace="sideBar">
            	<div class="accordionContent">没有操作权限</div></div>';
        }
		
	}
    
    //办公BBS的入口
    public function bbsmenu() {
        $where['mate_key'] = C('PROJECT_BBS_URL');
        $config2 = M('Config')->where($where)->find();
        $this->assign(C('PROJECT_BBS_URL') , $config2['mate_value']);
        $this->display();
        
    }
    
    //文章添加
    public function contentadd(){
        $this->display();
    }
    

	// 用户登录页面
	public function login() {
		if(empty($_REQUEST['username'])) {
		  $where['mate_key'] = C('PROJECT_NAME_CN');
		  $config = M('Config')->where($where)->find();
		  $this->assign('projectname' , $config['mate_value']);
		  $this->display();
		}else{
		  $this->redirect('Index/index');
		}
	}


	// 用户登出
    public function logout()
    {
        if(isset($_SESSION[C('USER_AUTH_KEY')])) {
			unset($_SESSION[C('USER_AUTH_KEY')]);
			unset($_SESSION);
			session_destroy();
            $this->redirect('Public/login');
        }else {
            $this->error('已经登出！');
        }
    }

	// 登录检测
	public function checkLogin() {
		if(empty($_POST['account'])) {
			$this->error('帐号必须！');
		}else if (empty($_POST['password'])){
			$this->error('密码必须！');
		}
        //生成认证条件
        $where            =   array();
		$where['account']	= $_POST['account'];
        $where['password']  = md5($_POST['password']);
        $where['status']	= 1;
        
        $user = M('User')->where($where)->find();
        
        if(!isset($user)) {
        	$this->error('账号密码错误！');
        }else{
            $_SESSION[C('USER_AUTH_KEY')]	= $user['id'];
            $_SESSION[C('USER_AUTH_NAME')]	= $user['nickname'];
            $_SESSION[C('USER_AUTH_VALUE')]	= $user['access'];
            $_SESSION['remark'] = $user['remark'];
            $where = array();
            $where['mate_key'] = C('PROJECT_NAME_EN');
            $config = M('Config')->where($where)->find();
            $_SESSION[C('PROJECT_NAME_EN')]= $config['mate_value']; 
            
            
    		$this->redirect('Index/index');
        }

	}

}